const fs = require('fs')

let MUS_JSON_DATA
let DYN_JSON_DATA

module.exports = async function data_Export() {
    function readMuseumFileFun() {
        let dataObj;
        return new Promise(function (resolve, reject) {
            fs.readFile('./MuseumData.json', function (err, data) {
                if (err) {
                    reject(err);
                }
                dataObj = JSON.parse(data.toString());
                resolve(dataObj);
            });
        })
    }

    await readMuseumFileFun().then(function (dataObj) {
        MUS_JSON_DATA = dataObj//成功返回
        console.log(MUS_JSON_DATA)
    }).catch(function (err) {
        console.log(err);
    })

    function readDynastyFileFun() {
        let dataObj;
        return new Promise(function (resolve, reject) {
            fs.readFile('./DynastyData.json', function (err, data) {
                if (err) {
                    reject(err);
                }
                dataObj = JSON.parse(data.toString());
                resolve(dataObj);
            });
        })
    }

    await readDynastyFileFun().then(function (dataObj) {
        DYN_JSON_DATA = dataObj//成功返回
        console.log(DYN_JSON_DATA)
    }).catch(function (err) {
        console.log(err);
    })

    const _objToStrMap = function (obj) {
        let strMap = new Map();
        for (let k of Object.keys(obj)) {
            strMap.set(k, obj[k]);
        }
        return strMap;
    }

    const MuseumMap = _objToStrMap(MUS_JSON_DATA)
    const DynastyMap = _objToStrMap(DYN_JSON_DATA)
    const _strMapToObj = function (strMap) {
        let obj = Object.create(null);
        for (let [k, v] of strMap) {
            obj[k] = v;
        }
        return obj;
    }

    let MuseumData = JSON.stringify(_strMapToObj(MuseumMap))
    let DynastyData = JSON.stringify(_strMapToObj(DynastyMap))
    // console.log(DynastyData)
    return[MuseumData,DynastyData]
}